package com.molyfun.parents.activity.coursecenter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.common.ShareDialogActivity;
import com.molyfun.parents.activity.myorder.OrderDetailActivity;
import com.molyfun.parents.activity.store.TeacherProfileActivity;
import com.molyfun.parents.rest.bean.TeacherDetailResponse;
import com.molyfun.parents.rest.bean.TeamListResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.uievent.UIEventListener;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.utils.TimeUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.carbs.android.expandabletextview.library.ExpandableTextView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:3/2/17.
 */

public class CourseGoodActivity extends BaseActivity implements View.OnClickListener, UIEventListener {

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.kaitong)
    TextView mKaitong;
    @InjectView(R.id.bottom_layout)
    LinearLayout mBottomLayout;
    @InjectView(R.id.product_share)
    ImageView mShare;
    @InjectView(R.id.product_price)
    TextView mProductPrice;
    @InjectView(R.id.product_count)
    TextView mProductCount;
    @InjectView(R.id.course_good_cover)
    ImageView mCourseLogo;
    @InjectView(R.id.class_time)
    TextView mClassTime;
    @InjectView(R.id.course_good_title)
    TextView mCourseGoodTitle;
    @InjectView(R.id.product_introduce)
    TextView mProductIntroduce;

    @InjectView(R.id.teacher_list_layout)
    LinearLayout mTeacherListLayout;

    private TeamListResponse.TeamBean mTeamBean;

    private static final String FLAG_TEAM_BEAN = "team_bean";

    //TODO 张欣提供分享详情页地址
    private String mProductId;

    public static final void startActivity(Context context, TeamListResponse.TeamBean teamBean) {
        Intent intent = new Intent();
        intent.setClass(context, CourseGoodActivity.class);
        intent.putExtra(FLAG_TEAM_BEAN, teamBean);
        context.startActivity(intent);
    }

    @Override
    public void handleUIEvent(Message msg) {
        switch (msg.what) {
            case EventDispatcherEnum.UI_EVENT_ALIPAY_SUCCESS:
            case EventDispatcherEnum.UI_EVENT_WECHAT_PAY_SUCCESS:
                finish();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_good);
        ButterKnife.inject(this);
        getSupportActionBar().setTitle("");

        mBack.setOnClickListener(this);
        mKaitong.setOnClickListener(this);
        mShare.setOnClickListener(this);

        mTeamBean = getIntent().getParcelableExtra(FLAG_TEAM_BEAN);
        if(mTeamBean != null){
            initTeacherListLayout(mTeamBean.getUserlist());
            if(mTeamBean.isPurchasestatus()){
                mBottomLayout.setVisibility(View.GONE);
            }

            if(!TextUtils.isEmpty(mTeamBean.getLogo())){
                Picasso.with(this)
                        .load(mTeamBean.getLogo())
                        .into(mCourseLogo);
            }

            mClassTime.setText("上课时间: " + TimeUtils.getDateByTimeMillis(mTeamBean.getBegindatetime()) + "~" + TimeUtils.getDateByTimeMillis(mTeamBean.getEnddatetime()));
            mCourseGoodTitle.setText(mTeamBean.getName());
            mProductIntroduce.setText(mTeamBean.getIntroduction());

        }

        mProductPrice.setText(mTeamBean.getPrice() + "");
        mProductCount.setText(mTeamBean.getSellcount() + "/" + mTeamBean.getNumber() + "人已报名");

        MFPApplication.getInstance().getEventController().addUIEventListener(EventDispatcherEnum.UI_EVENT_ALIPAY_SUCCESS, this);
        MFPApplication.getInstance().getEventController().addUIEventListener(EventDispatcherEnum.UI_EVENT_WECHAT_PAY_SUCCESS, this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.product_share:

                String shareUrl = "";

                switch (PreferencesStorageUtil.getServerMode(MFPApplication.getInstance())) {
                    case PreferencesStorageUtil.ServerMode.OFFICAL:
                        shareUrl = "http://h5.helloparents.molyfun.com/#/group/detail/" + mTeamBean.getId();
                        break;
                    case PreferencesStorageUtil.ServerMode.PREPARE:
                        shareUrl = "http://h5.helloparentstest.molyfun.com/#/group/detail/" + mTeamBean.getId();
                        break;
                    case PreferencesStorageUtil.ServerMode.TEST:
                        shareUrl = "http://192.168.3.30:8081/helloparents-client/#/group/detail/" + mTeamBean.getId();
                        break;
                }

                ShareDialogActivity.sharePage(CourseGoodActivity.this, mTeamBean.getName(), mTeamBean.getIntroduction(), shareUrl);
                break;
            case R.id.kaitong:
                OrderDetailActivity.startActivity(CourseGoodActivity.this, mTeamBean.getId());
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MFPApplication.getInstance().getEventController().removeUIEventListener(EventDispatcherEnum.UI_EVENT_ALIPAY_SUCCESS, this);
        MFPApplication.getInstance().getEventController().removeUIEventListener(EventDispatcherEnum.UI_EVENT_WECHAT_PAY_SUCCESS, this);
    }

    private void initTeacherListLayout(List<TeacherDetailResponse.TeacherDetailBean> teacherDetailBeanList) {
        mTeacherListLayout.removeAllViews();
        if (teacherDetailBeanList != null && teacherDetailBeanList.size() > 0) {
            int count = 0;
            for (final TeacherDetailResponse.TeacherDetailBean teacherDetailBean : teacherDetailBeanList) {
                View view = LayoutInflater.from(CourseGoodActivity.this).inflate(R.layout.layout_product_detail_teacher_cell, null, false);
                CircleImageView circleImageView = (CircleImageView) view.findViewById(R.id.avator);
                TextView teacherName = (TextView) view.findViewById(R.id.teacher_name);
                TextView teacherIdentity = (TextView) view.findViewById(R.id.teacher_identity);
                ExpandableTextView teacherIntroduce = (ExpandableTextView) view.findViewById(R.id.teacher_introduce);
                View mLine = view.findViewById(R.id.line);

                teacherName.setText(teacherDetailBean.getUsername());
                if (TextUtils.isEmpty(teacherDetailBean.getPost())) {
                    teacherIdentity.setVisibility(View.GONE);
                } else {
                    teacherIdentity.setVisibility(View.VISIBLE);
                    teacherIdentity.setText(teacherDetailBean.getPost());
                }

                if (TextUtils.isEmpty(teacherDetailBean.getSkills())) {
                    teacherIntroduce.setText("无");
                } else {
                    teacherIntroduce.setText(teacherDetailBean.getSkills());
                }

                if (!TextUtils.isEmpty(teacherDetailBean.getHeadimgurl())) {
                    Picasso.with(CourseGoodActivity.this)
                            .load(teacherDetailBean.getHeadimgurl())
                            .config(Bitmap.Config.RGB_565)
                            .placeholder(R.mipmap.ic_header_default)
                            .into(circleImageView);
                } else {
                    circleImageView.setImageResource(R.mipmap.ic_header_default);
                }
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TeacherProfileActivity.startActivity(CourseGoodActivity.this, teacherDetailBean);
                    }
                });
                mTeacherListLayout.addView(view);
                if (count == (teacherDetailBeanList.size() - 1)) {
                    mLine.setVisibility(View.GONE);
                } else {
                    mLine.setVisibility(View.VISIBLE);
                }

                count++;

            }
        }
    }

}
